package com.hadoop.mr.reducer;

import com.hadoop.mr.mapper.CommentMonthCountMapper;
import com.hadoop.mr.writable.CommentWritable;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created by Administrator on 2019/7/11.
 */
public class CommentMonthCountReducer extends Reducer<Text, CommentWritable, CommentWritable, CommentWritable> {

    private static Logger logger = LoggerFactory.getLogger(CommentMonthCountMapper.class);

    @Override
    protected void reduce(Text key, Iterable<CommentWritable> values, Context context)
            throws IOException, InterruptedException {
        try {
            Integer goodsSum = 0;
            String goodsType = "";
            for (CommentWritable bean : values) {
                if (StringUtils.isBlank(goodsType)) {
                    goodsType = bean.getGoodsType();
                }
                goodsSum += bean.getGoodsCount();
            }
            String array[] = key.toString().split(";");
            context.write(new CommentWritable(new Text(key).toString(), array[1], array[0], goodsSum, goodsType), null);
        } catch (Exception e) {
            logger.error("", e);
        }
    }


}